// express路由中间件

const express = require('express');
require("path");
const app = express();

// 声明中间件函数
function routeMiddleware(req, res, next) {
    if (req.query.code === '520') {
        next()
    } else {
        res.end('code error')
    }
}

// http://localhost:9018/home
// http://localhost:9018/home?code=520
app.get('/home', routeMiddleware, (req, res) => {
    res.send('hello home')
})

// http://localhost:9018/admin
// http://localhost:9018/admin?code=520
app.get('/admin', routeMiddleware, (req, res) => {
    res.send('hello admin')
})

app.all('*', (req, res) => {
    res.end('404 Not Found')
})

app.listen(9018, () => {
    console.log('express server running at http://127.0.0.1:9018')
})